package com.wangzhou.common.util;

import com.alibaba.fastjson.JSONObject;

/**
 * 分页对象
 * 
 * @author zjx
 *
 */
public class PageInfo
{
	// 总条数
	private int totalRecord;
	// 每页多少条
	private int pageSize = Integer.valueOf(ConstatFinalUtil.SYS_BUNDLE.getString("pageInfo.pageSize"));
	// 当前页
	private int currentPage = 1;
	// 总页数
	private int totalPage;
	//设置当前的条数
	private int currentRecord;
	// 上一页
	private int prePage;
	// 下一页
	private int nextPage;

	public int getTotalRecord()
	{
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getCurrentPage()
	{
		if(this.currentPage < 1)
		{
			this.currentPage = 1 ; 
		}
		if(this.currentPage > this.getTotalPage())
		{
			this.currentPage = this.getTotalPage() ; 
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}
	
	/**
	 * 根据总条数和每页多少条计算出来的
	 * @return
	 */
	public int getTotalPage()
	{
		if(this.getTotalRecord() % this.getPageSize() == 0)
		{
			//总共30条,每页10条,共3页(30/10)
			this.totalPage = this.getTotalRecord() / this.getPageSize() ;
		}else
		{
			//总共35条,每页10条,共4页(30/10+1)
			this.totalPage = this.getTotalRecord() / this.getPageSize() + 1 ; 
		}
		return totalPage;
	}

	public int getPrePage()
	{
		this.prePage = this.currentPage - 1 ; 
		if(this.prePage < 1)
		{
			this.prePage = 1 ; 
		}
		return prePage;
	}

	public int getNextPage()
	{
		this.nextPage = this.currentPage + 1 ; 
		if(this.nextPage > this.getTotalPage())
		{
			this.nextPage = this.getTotalPage() ; 
		}
		return nextPage;
	}

	public int getCurrentRecord()
	{
		this.currentRecord = (this.getCurrentPage() - 1 ) * this.pageSize ; 
		return currentRecord;
	}

	@Override
	public String toString()
	{
		return "PageInfo [totalRecord=" + totalRecord + ", pageSize=" + pageSize + ", currentPage=" + currentPage
				+ ", totalPage=" + totalPage + ", currentRecord=" + currentRecord + ", prePage=" + prePage
				+ ", nextPage=" + nextPage + "]";
	}
	
	/**
	 * 返回json对象
	 * @return
	 */
	public JSONObject toJSON()
	{
		JSONObject pageInfoJSON = new JSONObject();
		pageInfoJSON.put("totalRecord", this.getTotalRecord() + "");
		pageInfoJSON.put("pageSize", this.getPageSize() + "");
		pageInfoJSON.put("currentPage", this.getCurrentPage() + "");
		pageInfoJSON.put("totalPage", this.getTotalPage() + "");
		pageInfoJSON.put("prePage", this.getPrePage() + "");
		pageInfoJSON.put("nextPage", this.getNextPage() + "");
		return pageInfoJSON;
	}
}
